package org.jeecg.modules.kaoqtj.Job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.kaoqtj.entity.AgKaoqtj;
import org.jeecg.modules.kaoqtj.service.IAgKaoqtjService;
import org.jeecg.modules.tongxjl.entity.AgTongxjl;
import org.jeecg.modules.tongxjl.service.IAgTongxjlService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
public class syncKaoqtj implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            log.info(String.format("同步考勤统计，任务执行了，时间:" + DateUtils.formatDateTime()));
            long t1 = System.currentTimeMillis();
            syncTongxjl();
            long t2 = System.currentTimeMillis();
            log.info(" 同步考勤统计，任务执行结束了，执行时间:" + (t2-t1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Autowired
    private IAgTongxjlService agTongxjlService;

    @Autowired
    private IAgKaoqtjService agKaoqtjService;
    boolean yetFinishSync = false;

    public Result <?> syncTongxjl() throws Exception {
        System.out.println("考勤统计同步开始");
        // 如果上次没同步完，直接返回
        if (yetFinishSync) {
            System.out.println("yet over");
            return null;
        }

        String startTime = "2020-01-01 00:00:00";
        AgKaoqtj agKaoqtj = agKaoqtjService.getOne(new QueryWrapper<AgKaoqtj>().orderByDesc("xiabsj"), false);
        if (agKaoqtj != null) {
            startTime = DateUtils.date2Str(agKaoqtj.getXiabsj(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        }
        log.info("同步计算考勤统计中间值数据同步时间：startTime="+startTime);
        List<AgTongxjl> list = agTongxjlService.list(new QueryWrapper<AgTongxjl>().gt("tongxsj", startTime).eq("shenf", 1));
        yetFinishSync = true;

        for (AgTongxjl agTongxjl : list
        ) {
            String renybh = agTongxjl.getRenybh();


            if (renybh != null) {

                String tongxrq = DateUtils.date2Str(agTongxjl.getTongxsj(), new SimpleDateFormat("yyyy-MM-dd"));

                AgKaoqtj agKaoqtj1 = agKaoqtjService.getOne(new QueryWrapper<AgKaoqtj>().eq("renybh", renybh).likeRight("shangbsj", tongxrq), false);
                //  System.out.println("******" + "\n" + new QueryWrapper <AgKaoqtj>().eq("renybh", renybh).likeRight("shangbsj", tongxrq));
                if (agKaoqtj1 != null) {
                    if (agKaoqtj1.getXiabsj()==null||agKaoqtj1.getXiabsj().before(agTongxjl.getTongxsj())){
                        agKaoqtj1.setXiabsj(agTongxjl.getTongxsj());}
                    if (agKaoqtj1.getShangbsj().after(agTongxjl.getTongxsj())){
                        agKaoqtj1.setShangbsj(agTongxjl.getTongxsj());
                    }
//                    if ((agTongxjl.getJincfx()=="2"&&(agKaoqtj1.getXiabsj()==null))||(agTongxjl.getJincfx()=="2"&&agKaoqtj1.getXiabsj().before(agTongxjl.getTongxsj()))){
//                        agKaoqtj1.setXiabsj(agTongxjl.getTongxsj());}
//                    if (agTongxjl.getJincfx()=="1"&&agKaoqtj1.getShangbsj().after(agTongxjl.getTongxsj())){
//                        agKaoqtj1.setShangbsj(agTongxjl.getTongxsj());
//                    }
                  //  agKaoqtj1.setXiabsj(agTongxjl.getTongxsj());

                    agKaoqtjService.updateById(agKaoqtj1);

                } else {
                    agKaoqtj1 = new AgKaoqtj();
                    agKaoqtj1.setRenybh(agTongxjl.getRenybh());
                    agKaoqtj1.setXingm(agTongxjl.getXingm());
                    agKaoqtj1.setBum(agTongxjl.getBum());
                    agKaoqtj1.setShangbsj(agTongxjl.getTongxsj());
//                    agKaoqtj1.setGongh();
//                    agKaoqtj1.setZhiw();


                    agKaoqtjService.save(agKaoqtj1);
                }
            }


        }

        yetFinishSync = false;

        return null;
    }
}
